package org.example.context;

import io.netty.channel.ChannelHandlerContext;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.Consumer;

public class BaseContext implements IContext{
    protected final String protocol;// 转发协议

    protected volatile int status = IContext.Running; // 状态，多线程情况下考虑使用volatile

    protected final ChannelHandlerContext nettyCtx; // Netty上下文
    protected final Map<String, Object> attributes = new HashMap<>(); // 上下文参数
    protected Throwable throwable;//请求过程中发生的异常
    protected final boolean keepAlive;//是否保持长连接
    protected List<Consumer<IContext>> completedCallbacks; //存放回调函数集合

    protected  final AtomicBoolean requestReleased = new AtomicBoolean(false);//资源是否已经释放

    public BaseContext(String protocol, ChannelHandlerContext nettyCtx, boolean keepAlive) {
        this.protocol = protocol;
        this.nettyCtx = nettyCtx;
        this.keepAlive = keepAlive;
    }

    /**
     * 设置上下文状态为运行中
     */
    @Override
    public void runned() {
        status = IContext.Running;
    }

    /**
     * 设置上下文状态为标记写回
     */
    @Override
    public void writtened() {
        status = IContext.Written;

    }

    /**
     * 设置上下文状态为写回成功
     */
    @Override
    public void completed() {
        status = IContext.Completted;
    }

    /**
     * 设置上下文状态为请求结束
     */
    @Override
    public void terminated() {
        status = IContext.Terminated;
    }

    @Override
    public boolean isRunning() {
        return status == IContext.Running;
    }

    @Override
    public boolean isWrittened() {
        return status == IContext.Written;
    }

    @Override
    public boolean isCompleted() {
        return status == IContext.Completted;
    }

    @Override
    public boolean isTerminted() {
        return status == IContext.Terminated;
    }

    /**
     * 获取协议
     *
     * @return
     */
    @Override
    public String getProtocol() {
        return protocol;
    }

    /**
     * 获取请求对象
     *
     * @return
     */
    @Override
    public Object getRequest() {
        return null;
    }

    @Override
    public void setRequest(Object request) {

    }

    /**
     * 获取请求对象
     *
     * @return
     */
    @Override
    public Object getResponse() {
        return null;
    }

    @Override
    public void setResponse(Object object) {

    }

    @Override
    public void setThrowable(Throwable throwable) {
        this.throwable = throwable;
    }

    @Override
    public Throwable getThroable() {
        return throwable;
    }

    @Override
    public ChannelHandlerContext getNettyCtx() {
        return nettyCtx;
    }

    @Override
    public boolean isKeepAlive() {
        return keepAlive;
    }

    @Override
    public boolean releaseRequest() {
        return false;
    }

    @Override
    public void setCompletedCallBack(Consumer<IContext> consumer) {
        if(consumer == null) {
            completedCallbacks = new ArrayList<>();
        }
        completedCallbacks.add(consumer);
    }

    @Override
    public void invokeCompletedCallBack(Consumer<IContext> consumer) {
        if(completedCallbacks != null) {
            for (Consumer<IContext> completedCallback : completedCallbacks) {
                completedCallback.accept(this);
            }
        }
    }
}
